package com.nwcl.common.utils;

import com.nwcl.common.conf.WxYsxBeanConf;
import com.nwcl.common.session.SessionUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@SuppressWarnings("all")
@Component
public class SessionUtil {
    private static Logger log = LoggerFactory.getLogger(SessionUtil.class);
    private static int sessionTime = 24 * 30;// 单位小时
    private static String buildingId = "buildingId"; //前端缓存的楼宇ID
    private static String buildingName = "buildingName";//前端缓存的楼宇名称
    private static String toPage = "toPage";//从哪个页面(A)来到完善资料页面, 前端缓存的页面名称(A)
    @Autowired
    private WxYsxBeanConf wxYsxBeanConf;
    @Autowired
    private YsxSessionUtil ysxSessionUtil;
    @Autowired
    private RedisUtil redisUtil;

    /*
     * @Description:  获取当前用户信息
     * @Author liguiqin
     * @Date 2023/5/31 10:01
     * @param: request
     * @return com.nwcl.common.session.SessionUser
     * @UpdateDate 2023/5/31 10:01
     **/
    public SessionUser getUser(HttpServletRequest request) {
        String serverName = request.getServerName();
        if (wxYsxBeanConf.getYsxServername().equals(serverName)) {
            return ysxSessionUtil.getAccount(request);
        }
        return null;
    }

    /*
     * @Description:获取当前用户ID
     * @Author liguiqin
     * @Date 2023/5/31 10:02
     * @param: request
     * @return java.lang.Integer
     * @UpdateDate 2023/5/31 10:02
     **/
    public Integer getUserId(HttpServletRequest request) {
        String serverName = request.getServerName();
        if (wxYsxBeanConf.getYsxServername().equals(serverName)) {
            return ysxSessionUtil.getAccount(request).getId();
        }
        return null;
    }

    /*
     * @Description: 删除当前用户会话
     * @Author liguiqin
     * @Date 2023/5/31 10:02
     * @param: request
     * @param: response
     * @return void
     * @UpdateDate 2023/5/31 10:02
     **/
    public void deleteSession(HttpServletRequest request, HttpServletResponse response) {
        String serverName = request.getServerName();
        if (wxYsxBeanConf.getYsxServername().equals(serverName)) {
            ysxSessionUtil.deleteSession(request, response);
        }
    }

    /*
     * @Description:  更新当前用户会话
     * @Author liguiqin
     * @Date 2023/5/31 10:03
     * @param: request
     * @return boolean
     * @UpdateDate 2023/5/31 10:03
     **/
    public boolean updateSession(HttpServletRequest request) {
        String serverName = request.getServerName();
        if (wxYsxBeanConf.getYsxServername().equals(serverName)) {
            return ysxSessionUtil.updateSession(request);
        }
        return false;
    }

}
